﻿using BLL;
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class MemberInfoModify : Form
    {
        MeberTypeBLL _meberTypeBLL = new MeberTypeBLL();
        public MemberInfoModify()
        {
            InitializeComponent();
            //初始化修改信息
            InitModify();
        }

        public void InitModify()
        {
            //调用BLL层查询会员信息
            cmbMemType.DisplayMember = "tname";
            cmbMemType.ValueMember = "tid";
            cmbMemType.DataSource = _meberTypeBLL.SelectMemberTpye();


        }

        

        private void btnOk_Click(object sender, EventArgs e)
        {
            
            //接收会员信息
            MemberInfo memberInfo = new MemberInfo();

            memberInfo.Mid = Convert.ToInt32(txtMid.Text);
            memberInfo.Mname = txtMname.Text;
            memberInfo.Gender = rdoMan.Checked ? "男" : "女";
            //会员等级  
            memberInfo.Type = Convert.ToInt32(cmbMemType.SelectedValue);
            memberInfo.Discount = Convert.ToDouble(txtDisount.Text);
            memberInfo.Money = Convert.ToDouble(txtMoney.Text);
            memberInfo.Points = Convert.ToInt32(txtPoints.Text);
            memberInfo.Birthday = birDate.Value;
            memberInfo.RegistDate = registDate.Value;
            memberInfo.EndDate = endDate.Value;
            memberInfo.PhoneNum = txtPhoneNum.Text;


            //修改信息调用BLL
            MemberInfoBLL memberInfoBLL = new MemberInfoBLL();
            if(memberInfoBLL.UpdateMemberInfo(memberInfo) > 0)
            {
                //修改成功
                MessageBox.Show("修改成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        //订阅事件的方法
        public void GetValue(object sender, EventArgs e)
        {
            MemberInfo memberInfo = e as MemberInfo;
            txtMid.Text = memberInfo.Mid.ToString();
            txtMname.Text = memberInfo.Mname;
            txtMoney.Text = memberInfo.Money.ToString();
            txtDisount.Text = memberInfo.Discount.ToString();
            txtPoints.Text = memberInfo.Points.ToString();
            txtPhoneNum.Text = memberInfo.PhoneNum;
            registDate.Text = memberInfo.RegistDate.ToString();
            endDate.Text = memberInfo.EndDate.ToString();
            birDate.Text = memberInfo.Birthday.ToString();

            rdoMan.Checked = memberInfo.Gender == "男" ? true : false;
            rdoWomen.Checked = memberInfo.Gender == "女" ? true : false;
            cmbMemType.SelectedValue = memberInfo.Type;
        }
    }
}
